//
//  ViewController.m
//  键盘遮挡
//
//  Created by holier_zyq on 16/8/29.
//  Copyright © 2016年 holier_zyq. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>

@property (nonatomic ,strong) UIScrollView *scrollView;
@property (nonatomic ,strong) UILabel *placeholderLabel;
@property (nonatomic ,assign) NSInteger tags;

@end

@implementation ViewController

- (UILabel *)placeholderLabel{
    if (!_placeholderLabel) {
        self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 10)];
        _placeholderLabel.font = [UIFont systemFontOfSize:8];
//        _placeholderLabel.backgroundColor = [UIColor blueColor];
        
    }
    return _placeholderLabel;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.backgroundColor = [UIColor redColor];


    }
    return _scrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tags = 0;
    self.title = @"键盘遮挡";
    NSArray *array = @[@"占位字符",@"第二个",@"第三个",@"第四个",@"第五个",@"第六个"];
    for (int i = 0; i < 6; i++) {
        UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(17, i * 70 + 20, self.view.bounds.size.width - 34, 50)];
        textfield.tag = 100 + i;
        textfield.placeholder = array[i];
        textfield.returnKeyType = UIReturnKeyDone;
        textfield.delegate = self;
        textfield.backgroundColor = [UIColor cyanColor];
        [self.scrollView addSubview:textfield];
    }
    _scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, 500);
    [self.view addSubview:self.scrollView];
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
//    self.tags = textField.tag;
    
    CGRect frame = textField.frame;
    // 当前点击textfield的坐标的Y值 + 当前点击textFiled的高度 - （屏幕高度- 键盘高度 - 键盘上tabbar高度）
    // 在这一步 就是了一个 当前textfile的的最大Y值 和 键盘的最全高度的差值，用来计算整个view的偏移量
    //注意 : 当你的textField 处在一个ScrollView上时 必须要减去偏移量 不然你会发现 你的view 会销声匿迹的
    int offset = frame.origin.y + CGRectGetHeight(textField.frame) - self.scrollView. contentOffset .y- ( [UIScreen mainScreen].bounds.size.height - 216.0 - 64);//键盘高度216
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.view.frame.size.width;
    float height = self.view.frame.size.height;
    if(offset > 0)
    {
        CGRect rect = CGRectMake(0.0f, -offset,width,height);
        self.view.frame = rect;
    }
    [UIView commitAnimations];
    
    [self.placeholderLabel removeFromSuperview];
    [textField addSubview:self.placeholderLabel];
    _placeholderLabel.text = textField.placeholder;

    
    
}
//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.view endEditing:YES];
    
    NSTimeInterval animationDuration = 0.30f;
    
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    
    [UIView setAnimationDuration:animationDuration];
    
    CGRect rect = CGRectMake(0.0f, 20, self.view.frame.size.width, self.view.frame.size.height);
    
    self.view.frame = rect;
    
    [UIView commitAnimations];
}
//注意 在这个方法里 Y值为64  是因为我的视图中有NavigationBar 如果你的没有 可以为0
-(void)textViewDidEndEditing:(UITextView *)textView
{
    self.view.frame =CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height);
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
